using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

[System.Serializable]
[VolumeComponentMenuForRenderPipeline("Custom/VolumetricCloud", typeof(UniversalRenderPipeline))]
public class VolumetricCloud: VolumeComponent, IPostProcessComponent{
    
    [Tooltip("Base Color")]
    public ColorParameter lightColor = new ColorParameter(new Color(1, 1, 1, 1));
    public ColorParameter cloudColor = new ColorParameter(new Color(1, 1, 1, 1));
    [Tooltip("云朵基础色")]
    public ColorParameter DarknessThreshold = new ColorParameter(UnityEngine.Color.white);
    [Tooltip("云朵中间层颜色")]
    public ColorParameter CloudMidColor = new ColorParameter(UnityEngine.Color.white);
    [Tooltip("云朵中间层颜色偏移")]
    public FloatParameter CloudMidColorOffset = new ClampedFloatParameter(0.0f, 0.0f, 10.0f);
    [Tooltip("云朵深层颜色")]
    public ColorParameter CloudDeepColor = new ColorParameter(UnityEngine.Color.white);
    [Tooltip("云朵深层颜色偏移")]
    public FloatParameter CloudDeepColorOffset = new ClampedFloatParameter(0.0f, 0.0f, 10.0f);
    
    public FloatParameter shapeTiling = new FloatParameter(0.5f);
    public FloatParameter detailTiling = new FloatParameter(0.4f);
    // 云的最大和最小生成距离
    [Tooltip("Cloud Max Distance")] 
    public Vector3Parameter cloudMaxDis = new Vector3Parameter(Vector3.one);
    [Tooltip("Cloud Min Distance")] 
    public Vector3Parameter cloudMinDis = new Vector3Parameter(Vector3.zero);

    public TextureParameter weatherMap = new TextureParameter(null);
    public TextureParameter noise3D = new TextureParameter(null);
    [Tooltip("细节噪声")]
    public TextureParameter detailNoiseTex = new TextureParameter(null);
    [Tooltip("遮罩噪音")]
    public TextureParameter MaskNoise = new TextureParameter(null);
    [Tooltip("散射相位变换")]
    public Vector4Parameter PhaseParams = new Vector4Parameter(new Vector4(0.72f, 1, 0.5f, 1.58f));
    
    [Range(0.0f, 1.0f)]                                                                            //-0.17f, 27.17f, -3.65f, -0.08f
    public Vector4Parameter shapeNoiseWeights = new Vector4Parameter(new Vector4(-0.17f, 27.17f, -3.65f, -0.08f));
    //density
    public FloatParameter densityOffset = new FloatParameter(4.2f);
    [Range(0.0f, 1.0f)]  
    public FloatParameter cloudHeightWeight = new FloatParameter(0.81f);
    
    
    public Vector4Parameter xy_Speed_zw_Warp = new Vector4Parameter (new Vector4(0.05f, 1, 1, 10) );
    public bool IsActive() => true;
    public bool IsTileCompatible() => false;
    public void load(Material material, ref RenderingData data)
    {
        /* 将所有的参数载入目标材质 */
//half4 _lightColor;
//half4 _cloudColor;
        material.SetColor("_lightColor", lightColor.value);
        material.SetColor("_cloudColor", cloudColor.value);
        
        material.SetColor("_darknessThreshold", DarknessThreshold.value);
        material.SetColor("_CloudMidColor", CloudMidColor.value);
        material.SetColor("_CloudDeepColor", CloudDeepColor.value);
        material.SetFloat("_CloudMidColorOffset",CloudMidColorOffset.value);
        material.SetFloat("_CloudDeepColorOffset",CloudDeepColorOffset.value);
        //float _CloudMidColorOffset;
        //float _CloudDeepColorOffset;
        
        
        
        
        material.SetVector("_CloudMaxDis",cloudMaxDis.value);
        material.SetVector("_CloudMinDis",cloudMinDis.value);
        material.SetVector("_shapeNoiseWeights",shapeNoiseWeights.value);
        material.SetVector("_phaseParams",PhaseParams.value);
        material.SetVector("_xy_Speed_zw_Warp",xy_Speed_zw_Warp.value);
        material.SetFloat("_shapeTiling",shapeTiling.value);
        material.SetFloat("_detailTiling",detailTiling.value);
        material.SetFloat("_densityOffset",densityOffset.value);
        material.SetFloat("_cloudHeightWeight",cloudHeightWeight.value);
        material.SetTexture("_NoiseTex",noise3D.value);
        material.SetTexture("_detailNoiseTex",detailNoiseTex.value);
        
        material.SetTexture("_maskNoise",MaskNoise.value);
        
        material.SetTexture("_weatherMap",weatherMap.value);
    }
}

